package com.liu.wode.controller;

import com.liu.wode.mapper.DepartmentMapper;
import com.liu.wode.pojo.Department;
import com.liu.wode.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DepartmentController {
    @Autowired
    DepartmentMapper departmentMapper;

    @GetMapping("/departmentL")
    public String departmentList(Model model){
        List<Department> departments = departmentMapper.queryDepartmentList();
        model.addAttribute("departments",departments);
        return "department/dlist";
    }
    @GetMapping("/DtoAdd")
    public String toAdddep(){
        return "department/dadd";
    }
    @PostMapping("/DtoAdd")
    public String addEmp(@RequestParam("department") String department){
        int init=0;
        while (departmentMapper.queryDepartmentById(init)!=null){
            init++;
        }
        departmentMapper.addDepartment(new Department(init,department));
        return "redirect:/departmentL";
    }

    @GetMapping("/deldep/{id}")
    public String delUser(@PathVariable("id") Integer id){
        departmentMapper.delDepartment(id);
        return "redirect:/departmentL";
    }

    @GetMapping("/department/{id}")
    public String toUpdateUser(@PathVariable("id")Integer id,Model model){
        Department department = departmentMapper.queryDepartmentById(id);
        model.addAttribute("department",department);
        return "department/dupdate";
    }
    @PostMapping("/updateDep")
    public String updateUser(Department department){
        departmentMapper.upDepartment(department);
        return "redirect:/departmentL";
    }

}
